<template>
    <div class="login">
        <h3 align="center" >用户登录</h3>
        <el-form :model="loginuser" :rules="rules" ref="loginFrom">
            <el-form-item  prop="username">
                <el-input v-model="loginuser.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginuser.password"  placeholder="密码" type="password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-checkbox v-model="check">记住密码</el-checkbox>
            </el-form-item>
            <el-button type="primary" plain style="width: 100%;" @click="log">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    //import {postKeyValueRequest} from "../utils/api";

    export default {
        name: "Login",
        data(){
            return {
                rules:{
                    username: [
                        { required: true, message: '用户名不能为空', trigger: 'blur' }
                    ],
                    password:[{ required: true, message: '密码不能为空', trigger: 'blur'},
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }]
                },loginuser: {
                    username: '笛卡儿积',
                    password:'123'
                },
                check:true
            }
        },
        methods:{
            log(){
                this.$refs['loginFrom'].validate((valid) => {
                    if (valid) {
                        //alert('submit!');
                        this.postKeyValueRequest('/doLogin',this.loginuser).then((resp)=>{
                            if(resp!=null){
                                //this.$router.push()rellace是替换界面。push是不替换
                                window.sessionStorage.setItem('loginU',JSON.stringify(resp.object));//sessionStorage
                                //相当于放session的库，储存的key,value,JSON.stringify,将对象转换成我们能存的session形式
                                this.$router.replace('/home');//登录成功后跳界面
                            }
                            // else{
                            //     this.$message.error('请重新输入');
                            // }
                        });
                    } else {
                        this.$message.error('错了哦，这是一条错误消息');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
.login{
    background: lavenderblush;
    width: 330px;
    border: 1px solid lightyellow;
    padding: 10px;
    border-radius: 15px;
    text-align: center;
    margin: 120px auto;
    box-shadow: 0 0 10px pink;
}
</style>